#!/usr/bin/env python
#-*- coding: utf-8 -*-
#
# BSD License
# Copyright (c) 2011, Wang Qiang
# All rights reserved.

"""
Load config file to get config information
"""
from pyquery import PyQuery
from JQSelector.JQSelector import JQSelect, JQSelectPQ


class Config(object):
    """
    class for representing the config
    """
    def __init__(self, configFile):
        super(Config, self).__init__()
        self.configFile = configFile
        
    def load(self):
        """
        Load config information
        """
        self.config = open(self.configFile, 'r').read()
        self.loadLog()
        self.loadDatabase()

    def loadLog(self):
        """
        Load log config
        """
        log = JQSelectPQ(self.config, 'log')
        self.log = log[0].text()

    def loadDatabase(self):
        """
        Load database config
        """
        db = JQSelectPQ(self.config, 'database')
        self.dbtype = db[0].attr('type')
        db = str(db[0])
        host = JQSelectPQ(db, 'host')
        if host:
            self.host = host[0].text()
        else:
            self.host = "localhost"
        port = JQSelectPQ(db, 'port')
        if port:
            self.port = int(port[0].text())
        else:
            self.port = 10001
        dbname = JQSelectPQ(db, 'dbname')
        self.dbname = dbname[0].text()
        collection = JQSelectPQ(db, 'collection')
        self.collection = collection[0].text()
